import HelloWorld from "../components/HelloWorld.vue";
import {createRouter, createWebHashHistory} from "vue-router";
import Home from "../views/Home.vue";
import About from "../views/About.vue";
import BlogPost from "../views/BlogPost.vue";
import Mine from "../views/Mine.vue";

const routes = [
    { path: '/', component: Home},
    { path: '/my', component: Mine,  children: [
        {
            path: '/', component: HelloWorld
        },{
                path: '/post/:id', component: BlogPost
            }
        ]
    },
    { path: '/about', component: About},
    { path: '/todo', name: '我的待办', component: HelloWorld, meta: { requiresAuth: true } },
    { path: '/post/:id', component: BlogPost, meta: { requiresAuth: true } },
]

const router = createRouter({
    history: createWebHashHistory(),
    routes,
});

router.beforeEach((to, from, next) => {
    const jwt = localStorage.getItem("jwt");
    // 检查路由是否需要用户认证
    if (to.matched.some(record => record.meta.requiresAuth) && !jwt) {
        next({path: '/login'});
    } else {
        next();
    }
})
export default router;